Technical Q&A TX12
IsCmdChar

Q I found the following declaration in Script.h:

extern PASCAL Boolean IsCmdChar(const EventRecord *eventRecord, short test)

FOURWORDINLINE(0x2F3C, 0x8206, 0xFFD0, 0xA8B5);

I can't seem to find any documentation for this call; does such documentation exist? If this is what I think it is, it could be very useful.

A Whoops -- thanks for pointing this out. That routine was introduced with System 7. Here's a description of the routine:

FUNCTION IsCmdChar(keyEvent: EventRecord; testChar: INTEGER): BOOLEAN;

This function tests whether the Command key is being pressed in conjunction with another key (or keys) that could generate testChar for some combination of Command up or down and Shift up or down. This accommodates European keyboards that may have testChar as a shifted character, and non-Roman keyboards that will only generate testChar if Command is down. It's most useful for testing for Command-period, but it can test for command-AnyCharacter.

The caller passes in the event record, which is assumed by the function to be an event record for a key-down or auto-key event with the Command key down. The caller also passes in the character to be tested for (for example, "."). The function returns TRUE if testChar is produced with the current modifier keys, or if it would be produced by changing the current modifier key bits in either or both of the following ways:

  • turning the Command bit off
  • toggling the Shift bit
A sample of how you might use this function:
OSErr HandleKeys (EventRecord *eventPtr) {
     OSErr     err      = noErr;

if (eventPtr->modifiers & cmdKey) {
     if (IsCmdChar (eventPtr, '.')) {
         gStop = true;
     } else {
         err = DispatchMenuChoice (MenuKey (eventPtr->message & charCodeMask));
     }
   }

   return err;
}

Note: The IsCmdChar function returns the 0xFF to represent true. IsCmdChar does not return 1, so make sure that your code works correctly when 0xFF is returned. Don't test the function result against whavever happens to be defined as true by your development environment unless you know that true has been defined to be 0xFF. MacTypes.h defines true == 1, which would cause the expression to always evaluate as false.

[Jul 06 1998]


Developer Documentation | Technical Notes | Development Kits | Sample Code